package com.feiyumall.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feiyumall.account.dao.UserDao;
import com.feiyumall.account.dto.userManagerDto;
import com.feiyumall.account.entity.UserEntity;
import com.feiyumall.account.service.userService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Doate
 * @Date: Created in 2021/4/14 22:38
 * @Description:
 */

@Service
public class userServiceImpl implements userService {

    @Resource
    UserDao userDao;

    @Override
    public Map<String, Object> selectByParam(userManagerDto dto) {

        //设置参数条件
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();

        if (dto.getUserPower() != null) {
            wrapper.eq("user_power",dto.getUserPower());
        }
        if (dto.getDelFlag() == null) {
            wrapper.isNotNull("del_flag");
        } else {
            wrapper.eq("del_flag",dto.getDelFlag());
        }

        //设置分页查询
        IPage<UserEntity> iPage = userDao.selectPage(new Page<UserEntity>(dto.getPage(),dto.getLimit()),wrapper);
        List<UserEntity> list = iPage.getRecords();

        Map<String, Object> data = new HashMap<>();
        data.put("totalCount",iPage.getTotal());
        data.put("pageSize",iPage.getSize());
        data.put("totalPage",iPage.getPages());
        data.put("currPage",iPage.getCurrent());
        data.put("list",list);

        return data;
    }


    @Override
    public UserEntity searchById(int id) {

        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("del_flag").eq("user_id",id);
        UserEntity entity = userDao.selectOne(wrapper);

        return entity;
    }
}
